<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
                    <div class="tree" v-auth="item.type" v-for="item in group" :key="item.id" :class="{'active' : search.apply_cate == item.id }" @click="checkTree(item.id)">
                        <span>{{item.label}}</span>
                    </div>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" class="search-input" placeholder="供应商编号/名称/联系人/手机号" clearable @change="handleKeyDown"></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add" v-if="applyType=='factory.add'" v-auth="'factory.add'"></el-button>
					<el-button type="primary" icon="el-icon-plus" @click="add" v-if="applyType=='factoryProcess.add'" v-auth="'factoryProcess.add'"></el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'factory.alldel'"></el-button>
					<!-- <el-button type="primary" @click="bind" v-auth="'factory.bind'">绑定</el-button> -->
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border :params="query">
					<el-table-column type="selection" fixed  width="50" align="center"></el-table-column>
					<el-table-column label="ID" fixed  prop="apply_id" width="50" align="center"></el-table-column>
					<el-table-column label="编号" fixed  prop="apply_code" align="center"></el-table-column>
					<el-table-column label="名称" fixed  prop="apply_name" align="center"></el-table-column>
					<el-table-column label="简称" fixed  prop="referred_as" align="center"></el-table-column>
					<el-table-column label="类型" fixed  prop="apply_cate_name" align="center">
						<!-- <template #default="scope">
							<el-tag v-if="scope.row.apply_cate == 1" type="info">工厂供应商</el-tag>
							<el-tag v-if="scope.row.apply_cate == 2" type="danger">工序供应商</el-tag>
							<el-tag v-if="scope.row.apply_cate == 3" type="warning">毛料供应商</el-tag>
							<el-tag v-if="scope.row.apply_cate == 4" type="success">辅料供应商</el-tag>
						</template> -->
					</el-table-column>
					<el-table-column label="联系人" prop="linkman" align="center"></el-table-column>
					<el-table-column label="联系电话" prop="mobile" align="center"></el-table-column>
					<!-- <el-table-column label="状态" prop="system_disable" width="100" align="center">
						<template #default="scope">
							<el-switch v-model="scope.row.system_disable" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="0" :inactive-value="1"></el-switch>
						</template>
					</el-table-column> -->
					<el-table-column label="时间" prop="create_time" align="center"></el-table-column>
                    <el-table-column label="查看银行" align="center" width="100">
						<template #default="scope">
							<el-button text type="primary" size="small" @click="bank_show(scope.row, scope.$index)" v-auth="'factory.showbank'">查看</el-button>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="center" width="75">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'factory.show'">查看</el-button>
								<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'factory.edit'">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="danger" size="small" v-auth="'factory.delete'">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <bank v-if="dialog.bank" ref="bankDialog" @success="handleSuccess" @closed="dialog.bank = false" :apply_id="apply_id"></bank>
    <!-- fullscreen -->
</template>

<script>
import Bank from './bank';
import saveDialog from './save'

export default {
	name: 'user',
	components: {
		saveDialog,Bank,

	},
	data() {
		return {
			dialog: {
				save: false,
                bank:false,
                bind:false,
			},
			showGrouploading: false,
			groupFilterText: '',
			group: [
				{ id: 1, label: '工厂供应商',type:'factory.add' },
				{ id: 2, label: '工序供应商',type:'factoryProcess.add' },
			],
			apiObj: this.$API.basic.apply.lists,
            query:{
                apply_cate:this.$TOOL.data.get("PERMISSIONS").includes('factory.add') ? 1 : 2
            },
			selection: [],
			search: {
				keyword: null,
				apply_cate: ''
			},
            apply_id:"",
            applyType:''
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
		this.getGroup()
        this.applyType = this.$TOOL.data.get("PERMISSIONS").includes('factory.add') ? 'factory.add' : 'factoryProcess.add'
	},
	methods: {
        checkTree(item){
            this.search.apply_cate = item
            this.upsearch()
        },
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//表格内开关
		async changeSwitch(val, row) {
			row.$switch_status = true;
			var res = await this.$API.basic.apply.update_status.post({
				apply_id: row.apply_id,
				system_disable: row.system_disable,
			});
			if (res.code == 1) {
				row.$switch_status = false;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
        // 查看
        table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//查看银行
		bank_show(row) {
            console.log('row :>> ', row);
			this.dialog.bank = true
            this.apply_id = row.apply_id
			this.$nextTick(() => {
				this.$refs.bankDialog.open('add')
			})
		},
		//删除
		async table_del(row, index) {
			var reqData = { apply_id: row.apply_id }
			var res = await this.$API.basic.apply.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		async table_del_all(delete_ids) {
			var reqData = { ids: delete_ids }
			var res = await this.$API.basic.apply.deleteAll.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//加载树数据
		async getGroup() {
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.label.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data) {
			console.log(data, data)
			this.search.apply_cate = data.id
			this.upsearch()
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        handleKeyDown() {
            this.upsearch()
        },
	}
}
</script>

<style>
.search-input {
	width: auto;
	font-size: 14px;
}
.tree{
    width: 100%;
    font-size: 14px;
    padding: 10px 20px;
    color: #606266;
    cursor: pointer;
    user-select: none;
}
.active {
    background: #ebf5ff;
}
</style>
